<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图像分析管理</el-breadcrumb-item>
      <el-breadcrumb-item>识别记录</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加视图 -->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.pictureUrl" min-width="70" height="70" />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="结果">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="requestTime" label="分析时间"> </el-table-column>
        <el-table-column label="操作" width="100">
          <!-- 作用域插槽scope -->
          <template slot-scope="scope">
            <!-- 删除 -->
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-delete"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "AdminHistory",
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([ a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        return cb();
      } else {
        cb(new Error("请输入合法的邮箱"));
      }
    };

    return {
      // 获取用户的列表参数
      queryInfo: {
        // query: "",
        // 当前页数
        current: 1,
        // 当前每页显示多少条数据
        size: 5,
        userId: 1,
        // userId: window.sessionStorage.getItem("user"),
      },
      // 用户列表数据
      userlist: [],
      total: 0,
      addDialogVisiable: false,
      editDialogVisiable: false,
      // 添加用户的表单数据
      addForm: {
        name: "",
        password: "",
        email: "",
      },
      // 添加表单的验证规则对象
      addFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
      },
      // 查询到的用户信息对象
      editForm: {},
      editFormRules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
      },
      echartInstance: null,
      echartInstance2: null,
      allData: null,
      titleSize: 0,
      // 当前展示第一分类
      courrentIndex: 0,

      charData: [],
      charData2: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get(
        `api/record/all-list`,
        {
          params: this.queryInfo,
        }
      );
      if (res.code !== 200) {
        return this.$message.error(res.msg);
      } else {
        this.userlist = res.data.redords.records;
        this.allData = res.data.redords.records;
        this.total = res.data.total;
      }
      console.log(res);
    },
    // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getUserList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getUserList();
    },
    // 监听switch开关状态的改变
    async userStateChanged(userinfo) {
      const { data: res } = await this.$http.put(
        `users/${userinfo.id}/state/${userinfo.mg_state}`
      );
      if (res.meta.status !== 200) {
        userinfo.mg_state = !userinfo.mg_state;
        return this.$message.error(res.meta.msg);
      } else {
        this.$message.success(res.meta.msg);
      }
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 点击按钮，添加新用户
    addUser() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 可以发起用户的网络请求
        const { data: res } = await this.$http.post(
          "/api/user/back",
          this.addForm
        );
        if (res.status !== 200) {
          this.$message.error(res.msg);
        } else {
          this.$message.success(res.msg);
          this.addDialogVisiable = false;
          // 重新获取用户列表
          this.getUserList();
        }
      });
    },
    async showEditDialog(id) {
      const { data: res } = await this.$http.get(`/api/user/back/${id}`);
      if (res.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.editForm = res.obj;
      this.editDialogVisiable = true;
    },
    editUserInfo() {
      console.log("修改用户");
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        // 可以发起修改用户的网络请求
        const { data: res } = await this.$http.put(`/api/user/`, {
          id: this.editForm.id,
          email: this.editForm.email,
          name: this.editForm.name,
        });
        if (res.status !== 200) {
          return this.$message.error(res.msg);
        } else {
          this.$message.success(res.msg);
          this.editDialogVisiable = false;
          // 重新获取用户列表
          this.getUserList();
        }
      });
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    async removeUserById(id) {
      // 弹框询问是否确认删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该记录, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete("/api/user/back/", {
        params: {
          userId: id,
        },
      });
      if (res.status !== 200) {
        return this.$message.error(res.msg);
      } else {
        this.$message.success(res.msg);
        // 重新获取用户列表
        this.getUserList();
      }
    },
  },
};
</script>

<style>
</style>